class Solution(object):
    def isSelfCrossing(self, distance):
        """
        :type distance: List[int]
        :rtype: bool
        """
        n = len(distance)
        if n <= 3:
            return False
        points = [[0, 0], [0, distance[0]], [-distance[1], distance[0]], [-distance[1], distance[0] - distance[2]]]
        index = 3
        next_id = 0
        now_point = [points[index % 4][0] + distance[index], points[index % 4][1]]
        last_point = points[0]
        if now_point[1] >= points[next_id][1]:
            if now_point[0] >= points[next_id][0]:
                return True
            else:
                points[next_id] = now_point
        else:
            if now_point[0] == points[next_id][0]:
                points[next_id + 1] = points[next_id]
            points[next_id] = now_point
        index += 1
        while index < n:
            next_id = (index + 1) % 4
            if next_id == 0:
                now_point = [points[index % 4][0] + distance[index], points[index % 4][1]]
                if now_point[1] >= points[next_id][1]:
                    if now_point[0] >= points[next_id][0]:
                        return True
                    else:
                        points[next_id] = now_point
                else:
                    if last_point[0] <= now_point[0] <= points[next_id][0]:
                        points[next_id + 1] = points[next_id]
                    else:
                        last_point = points[next_id]
                    points[next_id] = now_point
            if next_id == 1:
                now_point = [points[index % 4][0], points[index % 4][1] + distance[index]]
                if now_point[0] <= points[next_id][0]:
                    if now_point[1] >= points[next_id][1]:
                        return True
                    else:
                        points[next_id] = now_point
                else:
                    if last_point[1] <= now_point[1] <= points[next_id][1]:
                        points[next_id + 1] = points[next_id]
                    else:
                        last_point = points[next_id]
                    points[next_id] = now_point
            if next_id == 2:
                now_point = [points[index % 4][0] - distance[index], points[index % 4][1]]
                if now_point[1] <= points[next_id][1]:
                    if now_point[0] <= points[next_id][0]:
                        return True
                    else:
                        points[next_id] = now_point
                else:
                    if last_point[0] >= now_point[0] >= points[next_id][0]:
                        points[next_id + 1] = points[next_id]
                    else:
                        last_point = points[next_id]
                    points[next_id] = now_point
            if next_id == 3:
                now_point = [points[index % 4][0], points[index % 4][1] - distance[index]]
                if now_point[0] >= points[next_id][0]:
                    if now_point[1] <= points[next_id][1]:
                        return True
                    else:
                        points[next_id] = now_point
                else:
                    if last_point[1] >= now_point[1] >= points[next_id][1]:
                        points[0] = points[next_id]
                    else:
                        last_point = points[next_id]
                    points[next_id] = now_point
            index += 1
        return False


data = Solution()
distance = [2, 1, 1, 2]
print(data.isSelfCrossing(distance))
distance = [1, 2, 3, 4]
print(data.isSelfCrossing(distance))
distance = [1, 1, 1, 1]
print(data.isSelfCrossing(distance))
distance = [2, 2, 2, 1]
print(data.isSelfCrossing(distance))
distance = [3, 3, 4, 2, 2]
print(data.isSelfCrossing(distance))
distance = [1, 1, 3, 2, 1, 1]
print(data.isSelfCrossing(distance))
